import logging
import smtplib
from email.mime.text import MIMEText

logger = logging.getLogger('django')


async def send_email(username, email, re_email, password, bug_name):
    try:
        server = smtplib.SMTP_SSL('smtp.exmail.qq.com', 465)
        server.connect('smtp.exmail.qq.com', 465)
        server.login(email, password)
        username = str(username)
        bug_name = str(bug_name)
        email = str(email)
        re_email = str(re_email)
        bug_name = str(bug_name)
        password = str(password)
        # 设置邮件主题和内容
        subject = 'Bug新增提醒'
        body = username + '新建了一条Bug给你，请及时处理，缺陷名称是：' + bug_name
        # 创建一个MIMEText对象，并设置邮件正文和编码格式
        message = MIMEText(body)
        message['Subject'] = subject
        message['From'] = email
        message['To'] = ','.join([re_email])
        message['CC'] = '抄送'  # 如果需要抄送其他人，可以在这里添加其他收件人的邮箱地址
        message['X-Priority'] = '1'  # 设置邮件优先级为高
        message['X-MSMail-Priority'] = 'High'  # 同上
        message['Content-Type'] = 'text/plain; charset=utf-8'  # 设置邮件正文的编码格式

        server.sendmail(email, ','.join([re_email]), message.as_string())
        logger.info('邮件发送成功')

        # 断开与SMTP服务器的连接
        server.quit()
        # 连接到SMTP服务器并发送邮件

    except Exception as e:
        logger.error(e)